package org.filetransfer.file_transfer_java_backend.repository;
import org.filetransfer.file_transfer_java_backend.entity.File;
import org.filetransfer.file_transfer_java_backend.model.ServiceFileInfoModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
@Repository
public interface FileRepository extends JpaRepository<File, Long> {
    Optional<File> findById(Long id);
    List<File> findByShareItem_Id(Long shareItemId);
    File findByMd5(String md5);
    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceFileInfoModel(" +
            "count(f), " +
            "sum(f.fileSize), " +
            "sum(f.downloadCount), " +
            "sum(f.downloadCount * f.fileSize)" +
            ") from File f")
    Optional<ServiceFileInfoModel> getStatistics();
    //    public class FileCategoryModel {
//        public enum FileType {
//            IMAGE, VIDEO, AUDIO, DOCUMENT, OTHER
//        }
//        @Schema(description = "文件类型")
//        private org.filetransfer.file_transfer_java_backend.model.FileCategoryModel.FileType fileType;
//        @Schema(description = "文件数量")
//        private long fileCount;
//        @Schema(description = "文件总大小")
//        private long totalSize;
//    }
//    @Query("select new org.filetransfer.file_transfer_java_backend.model.FileCategoryModel(" +
//            "f.fileName, " +
//            "count(f), " +
//            "sum(f.fileSize)" +
//            ") from File f")
//    List<FileCategoryModel> getFileCategoryStatistics();
    @Query("select f.fileName, f.fileSize from File f")
    List<Object[]> getFileDetails();

    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceFileInfoModel(" +
            "count(f), " +
            "sum(f.fileSize), " +
            "sum(f.downloadCount), " +
            "sum(f.downloadCount * f.fileSize)" +
            ") from File f where f.uploadTime >= :start and f.uploadTime < :end")
    Optional<ServiceFileInfoModel> getStatisticsInRange(Instant start, Instant end);

    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceFileInfoModel(" +
            "count(f), " +
            "sum(f.fileSize), " +
            "sum(f.downloadCount), " +
            "sum(f.downloadCount * f.fileSize)" +
            ") from File f where f.shareItem.user.id = :userId")
    Optional<ServiceFileInfoModel> getStatisticsByUser(Long userId);

    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceFileInfoModel(" +
            "count(f), " +
            "sum(f.fileSize), " +
            "sum(f.downloadCount), " +
            "sum(f.downloadCount * f.fileSize)" +
            ") from File f where f.uploadTime >= :start and f.uploadTime < :end and f.shareItem.user.id = :userId")
    Optional<ServiceFileInfoModel> getStatisticsInRangeByUser(Instant start, Instant end, Long userId);

    @Query("select f.fileName, f.fileSize from File f where f.shareItem.user.id = :userId")
    List<Object[]> getFileDetailsByUser(Long userId);
}
